package cn.itcast.n4_共享模型_管程;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

/**
 * @program: juc
 * @description: -XX:-UseBiasedLocking
 * @author: flyduck
 * @create: 2024-09-25 16:09
 **/
@Slf4j(topic = "c.Test17_偏向锁撤销方式2_其他线程使用对象")
public class Test17_偏向锁撤销方式2_其他线程使用对象 {
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(4000);
        Dog dog = new Dog();

        new Thread(() -> {
            log.debug("加锁前对象的mark word:{}",ClassLayout.parseInstance(dog).toPrintableSimple());//00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000101

            synchronized (dog) {
                log.debug("加锁后对象的mark word:{}",ClassLayout.parseInstance(dog).toPrintableSimple());//00000000 00000000 00000000 00000000 00011111 11110101 11110000 00000101
            }

            log.debug("解锁后对象的mark word:{}",ClassLayout.parseInstance(dog).toPrintableSimple());//00000000 00000000 00000000 00000000 00011111 11110101 11110000 00000101

            synchronized (Test17_偏向锁撤销方式2_其他线程使用对象.class) {
                Test17_偏向锁撤销方式2_其他线程使用对象.class.notify();
            }
        },"t1").start();

        new Thread(() -> {
            synchronized (Test17_偏向锁撤销方式2_其他线程使用对象.class) {
                try {
                    Test17_偏向锁撤销方式2_其他线程使用对象.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            log.debug("加锁前对象的mark word:{}",ClassLayout.parseInstance(dog).toPrintableSimple());//00000000 00000000 00000000 00000000 00011111 11110101 11110000 00000101

            synchronized (dog) {
                log.debug("加锁后对象的mark word:{}",ClassLayout.parseInstance(dog).toPrintableSimple());//00000000 00000000 00000000 00000000 00100000 01010111 11110010 11110000
            }

            log.debug("解锁后对象的mark word:{}",ClassLayout.parseInstance(dog).toPrintableSimple());//00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001
        },"t2").start();


    }
}


